#!/bin/sh
# 防止重复启动
LOCK=/var/lock/adblock.lock
if [ -f $LOCK ];then
	case $1 in
		gen|addown)X=1;;
		*)exit 1;;
	esac
fi
touch $LOCK

A="Download Adblock Plus Rules"
B="Download Subscribe Rules"
C=0
D=0
E="date +'%Y-%m-%d %H:%M:%S'"
U=`uci -q get adblock.@adblock[0].url`
P=/tmp/adblock
W=`cat /etc/adblock/white.list`

gen(){
	cat /tmp/adnew.conf | grep ^\|\|[^\*]*\^$ | grep -Ev "^\|\|[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}*" | sed -e 's:||:address=/:' -e 's:\^:/:' > /tmp/ad.conf
	for i in $W;do sed -i -e "/\/$i\//d" -e "/\.$i\//d" /tmp/ad.conf;done
	rm -f /tmp/adnew.conf
}

down(){
	G=/tmp/ad_tmp/3rd
	F=$G/ad_new.conf
	rm -rf ${G%/*}
	mkdir -p $G
	for i in $U;do
		X=1
		while ! curl --connect-timeout 5 -m 20 -Lfso $F $i;do
			[ $X -ge 20 ] && echo "`eval $E` [Download $i Failed]" && continue 2 || let X++
			sleep 2
		done
		X=`md5sum $G/3rd.conf 2>/dev/null | awk '{print$1}'`
		Y=`md5sum $G/host 2>/dev/null | awk '{print$1}'`
		sed -i 's/\r/\n/g' $F;sed -i -e '/127.0.0.1 #/d' -e '/127.0.0.1 !/d' -e 's:#.*::' -e 's:!.*::' -e 's/\$.*//g' -e 's/[ \t]*$//g' -e 's/^[ \t]*//g' -e '/\*/d' -e '/^$/d' $F
		if grep -q "^address=" $F;then
			cat $F >> $G/3rd.conf
		elif grep -q -e "^0.0.0.0 " -e "^127.0.0.1 " $F;then
			cat $F >> $G/host
		elif ! grep -q -e "|" -e "@" $F;then
			cat $F | sed -e 's:^:address=/:' -e 's:$:/:' >> $G/3rd.conf
		else
			cat $F | grep ^\|\|[^\*]*\^$ | grep -Ev "^\|\|[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}*" | sed -e 's:||:address=/:' -e 's:\^:/:' >> $G/3rd.conf
		fi
		[ "$X" = "`md5sum $G/3rd.conf 2>/dev/null | awk '{print$1}'`" -a "$Y" = "`md5sum $G/host 2>/dev/null | awk '{print$1}'`" ] && echo "`eval $E` [Conversion $i Failed]"
		echo $i >> $G/url
	done
	[ -s $G/host ] && sed -e '/:/d' -e '/ 0.0.0.0/d' -e '/255.255.255.255/d' -e '/ local/d' -e 's:127.0.0.1 :address=/:' -e 's:0.0.0.0 :address=/:' -e 's:$:/:' $G/host >> $G/3rd.conf
	[ -s $G/3rd.conf ] && sed -i -e 's:/127.0.0.1$:/:' -e 's:/0.0.0.0$:/:' $G/3rd.conf && echo "`sort -u $G/3rd.conf`" > $G/3rd.conf
	[ -s $G/url ] && echo "`sort -u $G/url`" > $G/url
	if [ -s $G/3rd.conf -a -s $P/adblock.conf ];then
		echo "`sort -u $G/3rd.conf $P/adblock.conf`" > $G/3rd.conf
		echo "`sort $G/3rd.conf $P/adblock.conf | uniq -u`" > $G/3rd.conf
	fi
	if [ -s $G/3rd.conf ];then
		echo "`eval $E` [$B Successful]"
		rm -f $F $G/host $P/failed
		for i in $W;do sed -i -e "/\/$i\//d" -e "/\.$i\//d" $G/3rd.conf;done
		[ "$1" = 2 ] && rm -f $LOCK && exit
		X=`uci -q get adblock.@adblock[0].flash`
		Y=`md5sum $G/* | awk '{print$1}'`
		[ $X = 0 ] && Z=`md5sum $P/3rd/* 2>/dev/null | awk '{print$1}'` || Z=`md5sum /etc/adblock/3rd/* 2>/dev/null | awk '{print$1}'`
		if [ "$Y" != "$Z" ];then
			[ "$1" = 1 ] || echo "`eval $E` [Subscribe Rules Need Update]"
			if [ "$X" = 0 ];then
				rm -f $P/3rd/*
				cp -a $G $P
			else
				rm -f /etc/adblock/3rd/*
				cp -a $G /etc/adblock
			fi
			D=1
		else
			echo "`eval $E` [Subscribe Rules No Change]"
		fi
	else
		echo "`eval $E` [$B Failed]"
		echo failed > $P/failed
		[ "$1" = 2 ] && rm -f $LOCK && exit
	fi
	rm -rf ${G%/*}
}

case $1 in
	addown)down 1;exit;;
	down)down 2;;
	gen)gen;[ "$X" = 1 ] || rm -f $LOCK;exit;;
esac

if [ `uci -q get adblock.@adblock[0].enable` = 1 ];then
	G=`curl -LSfso /tmp/adnew.conf https://cdn.jsdelivr.net/gh/small-5/ad-rules/easylistchina+easylist.txt 2>&1 || curl -LSfso /tmp/adnew.conf https://easylist-downloads.adblockplus.org/easylistchina+easylist.txt 2>&1`
	if [ $? = 0 ];then
		echo "`eval $E` [$A Successful]"
		gen
		if ! cmp -s /tmp/ad.conf $P/adblock.conf;then
			echo "`eval $E` [Adblock Plus Rules Need Update]"
			mv -f /tmp/ad.conf $P/adblock.conf
			C=1
		else
			echo "`eval $E` [Adblock Plus Rules No Change]"
		fi
	else
		echo "`eval $E` [$A Failed]"
		echo -e "$G\n"
	fi
	rm -f /tmp/ad.conf
	[ -n "$U" ] && down
	echo `eval $E` > $P/adblock.updated
fi

if [ $C = 1 -o $D = 1 ];then
	echo "`eval $E` [Reload Adblock Plus Rules]"
	/etc/init.d/adblock restart
fi

rm -f $LOCK
